home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3973 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem...Problem!
  5. Date: 1 Feb 96 00:53:10 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.823135990@hubcap>
  8. References: <4el09q$6im@ratree.psu.ac.th> <310FB217.280C@microsports.com>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. "Superstition ain't the way!"
  12.  
  13.         ---Stevie Wonder
  14.  
  15. "Eric W. Bradway" <ebradway@microsports.com> thoughtfully analyzes
  16. a program posted by Sanon CHAOCHAIYAPORN:
  17.  
  18. >> #define NUM 8
  19. >> #define ERR 1e-4
  20.  
  21. >Try avoiding the scientific notation by doing a divide by 10000 and 
  22. >comparing to 1. I assume this isn't a time-critical routine so the extra 
  23. >divide won't hurt. If it is time-critical, rewrite the program using 
  24. >long ints.
  25.  
  26. What's your reasoning here?  What's wrong with a perfectly good double
  27. constant that is improved by doing the division, and what on earth is
  28. the point of using long ints?
  29.  
  30. >>     v[i]   = (v[i+1] + v[i+2]) / 8;
  31. >>     v[i+1] = (v[i] + v[i+3] + 40) / 8;
  32. >>     v[i+2] = (v[i] + v[i+3] + v[i+5] + 50) / 8;
  33. >>     v[i+3] = (v[i+1] + v[i+2] + 110) / 8;
  34. >>     v[i+4] = (v[i+5] + v[i+6] + 150) / 8;
  35. >>     v[i+5] = (v[i+2] + v[i+4] + v[i+7] + 70) / 8;
  36. >>     v[i+6] = (v[i+4] + v[i+7] + 200) / 8;
  37. >>     v[i+7] = (v[i+5] + v[i+6] + 200) / 8;
  38.  
  39. >Your problem may lie here - add a .0 to each numeric constant (e.g., 
  40. >8.0, 40.0, 110.0) to get the compiler to treat them as floats. Many 
  41. >compilers automatically convert everything to ints if you don't 
  42. >specifically show that the values/variables are floats.
  43.  
  44. Since v[] is an array of floats, all these computations will be done
  45. as floats.  Every (working) compiler will do this correctly.  Every
  46. (working) compiler will do the arithmetic with integral types if and
  47. only if all the operands are integral types.
  48.  
  49. >And of course, on closer inspection, when do you assign meaningful 
  50. >values to v[]? Do you mean to initialize to zero and get the following 
  51. >results on the first pass:
  52.  
  53. He does mean this, and he says so in a loop before the main loop starts.
  54.  
  55. >[...]
  56.  
  57. Did you think about the changes you suggest, and what effect they
  58. might have?  Did you even try them out before posting?
  59. -- 
  60.         Matthew Saltzman
  61.         Clemson University Math Sciences
  62.         mjs@clemson.edu
  63.